from src.database.connection import Neo4jInstance
from src.ifc.util import util
from py2neo import Node

from src.ifc.util.util import mid_insert_node_with_tx, suf_insert_node

graph = Neo4jInstance.get_connect()


# 模板拆解(柱、梁、楼板)
# def disassemble_board(storeys):
#     for index, storey in enumerate(storeys):
#         # 查找这个楼层的所有 柱、梁、楼板
#         components = graph.run(
#             """
#                 MATCH (c)-[:PLACED_IN]->(s:IfcBuildingStorey {GlobalId: $storey_id})
#                 WHERE c.type in ["IfcColumn", "IfcBeam", "IfcSlab"]
#                 RETURN c
#             """,
#             storey_id=storey["GlobalId"],
#         ).data()
#         components = [component["c"] for component in components]
#
#         # 建立  当前构建的模板拆解 是 下层建筑 的前置条件
#         if index < len(storeys) - 1:
#             next_storey_id = storeys[index + 1]["GlobalId"]
#             tx = graph.begin()
#             for component in components:
#                 component_id = component["GlobalId"]
#                 global_id = util.generate_virtual_id()
#                 volume = 7
#                 # TODO 悬臂构件 是什么？
#                 # 跨度小于 8 米的梁、板，混凝土浇筑至少10天后才能拆模；
#                 # 跨度大于 8 米的梁、板和悬臂构件，至少14天才能拆模。
#                 if component["type"] == "IfcBeam" or component["type"] == "IfcSlab":
#                     if component["span"] < 8000:
#                         volume = 10
#                     else:
#                         volume = 14
#                 node = Node(
#                     "Virtual", name="模板拆解", volume=volume, GlobalId=global_id
#                 )
#                 tx.create(node)
#
#                 mid_insert_node_with_tx(component_id, global_id, next_storey_id, tx)
#             tx.commit()
#         else:
#             tx = graph.begin()
#             for component in components:
#                 component_id = component["GlobalId"]
#                 global_id = util.generate_virtual_id()
#                 suf_insert_node(component_id, global_id, tx)
#             tx.commit()
